package com.xinruke.hostels.dailymanage.service;

import com.alibaba.fastjson.JSONObject;
import com.xinruke.hostels.common.cache.ManageUserCache;
import com.xinruke.hostels.common.constants.RedisConstants;
import com.xinruke.hostels.common.constants.SysDicConstant;
import com.xinruke.hostels.common.exception.BizException;
import com.xinruke.hostels.common.mybatis.MyExample;
import com.xinruke.hostels.common.po.*;
import com.xinruke.hostels.common.po.constants.*;
import com.xinruke.hostels.common.service.OperateLogService;
import com.xinruke.hostels.common.util.CustomSecurity;
import com.xinruke.hostels.common.vo.ResultVO;
import com.xinruke.hostels.common.vo.query.EConditionOperator;
import com.xinruke.hostels.common.vo.query.QueryConditions;
import com.xinruke.hostels.dailymanage.interfaces.dto.inhotel.TeamInHotelDTO;
import com.xinruke.hostels.dailymanage.interfaces.dto.inhotel.TeamInHotelResultDTO;
import com.xinruke.hostels.dailymanage.interfaces.vo.TeamInHotelRoomVO;
import com.xinruke.hostels.dailymanage.interfaces.vo.TogetherInHotelInfoVO;
import com.xinruke.hostels.dailymanage.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.weekend.Weekend;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @ClassName TeamRoomService
 * @CreateDate 2019/7/15
 * @Author FengXinQiang
 * @Version V1.0.0.0
 * @Decription 团队房间Service
 */
@Service
@Slf4j
public class TeamRoomService {
    @Autowired
    YwFangjianMapper fangjianMapper;

    @Autowired
    YwFangjianleixingMapper fangjianleixingMapper;

    @Autowired
    YwFangtairiqijiluMapper fangtairiqijiluMapper;

    @Autowired
    GuestService guestService;

    @Autowired
    YwYudingDingdanMapper yudingDingdanMapper;

    @Autowired
    YwYudingDingdanZixiangMapper yudingDingdanZixiangMapper;

    @Autowired
    YwRuzhuDingdanMapper ruzhuDingdanMapper;

    @Autowired
    YwRuzhuDingdanZixiangMapper ruzhuDingdanZixiangMapper;

    @Autowired
    YwFangjiantongzhurenMapper fangjiantongzhurenMapper;

    @Autowired
    YwDingdanshouzhijiluMapper dingdanshouzhijiluMapper;

    @Autowired
    OperateLogService operateLogService;

    @Autowired
    private ManageUserCache manageUserCache;

    @Resource(name = "commonRedisTemplate")
    RedisTemplate commonRedisTemplate;

    /**
     * 入住
     * <p>
     * 1.Redis加锁
     * 2.保存宾客信息
     * 3.查找对应的预订单并修改为已入住
     * 4.写入订单表
     * 5.有预订单，解锁房间
     * 6.锁定房间
     * 7.写入订单子表
     * 8.保存宾客信息
     * 9.写入同住人信息表
     * 10.修改房间表状态
     * 11.写入订单收支记录
     * 12.写入操作日志
     *
     * @param teamInHotelDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResultVO<TeamInHotelResultDTO> inHotel(TeamInHotelDTO teamInHotelDTO) {
        String traceId = CustomSecurity.getTraceID();
        log.info("traceId[" + traceId + "]" + JSONObject.toJSONString(teamInHotelDTO));

        ResultVO<TeamInHotelResultDTO> resultVO = ResultVO.fail();

        teamInHotelDTO.setZhengjianhao(CustomSecurity.encodeIDCard(teamInHotelDTO.getZhengjianhao()));
        teamInHotelDTO.setYudingrendianhua(CustomSecurity.encodeMobile(teamInHotelDTO.getYudingrendianhua()));

        int jiudianId = manageUserCache.getJiudianId();
        String redisLockKey = RedisConstants.HOTEL_ROOM_LOCK_KEY_PREFIX + jiudianId;
        try {
            //使用redis，根据酒店id对房间操作加锁
            boolean lockRet = commonRedisTemplate.opsForValue().setIfAbsent(redisLockKey, jiudianId);
            if (!lockRet) {
                log.error("traceId[" + traceId + "]房间冲突，请重试");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultVO.setMessage("房间冲突，请重试");
                return resultVO;
            }

            String dingdanhao = YwRuzhuDingdanConstants.DINGDANHAO_PREFIX + System.currentTimeMillis();

            //保存宾客信息
            YwZhengjianxinxi zhengjianxinxi = new YwZhengjianxinxi();
            zhengjianxinxi.setShoujihao(teamInHotelDTO.getYudingrendianhua());
            zhengjianxinxi.setXingming(teamInHotelDTO.getYudingrenxingming());
            zhengjianxinxi.setZhengjianhao(teamInHotelDTO.getZhengjianhao());
            zhengjianxinxi.setZhengjianleixing(teamInHotelDTO.getZhengjianleixing());
            zhengjianxinxi.setJiudianId(jiudianId);
            zhengjianxinxi.setYewuleixing(YwZhengjianxinxiConstants.YEWULEIXING_QIANTAIYUDINGDAN);
            zhengjianxinxi.setYewulaiyuanId(-1);
            int guestId = guestService.saveGuest(zhengjianxinxi);

            //查找对应的预订单
            Integer yudingId = teamInHotelDTO.getYudingId();
            YwYudingDingdan yudingDingdan = null;
            if (yudingId != null && yudingId != 0) {
                QueryConditions dingdanQueryCondition = new QueryConditions();
                dingdanQueryCondition.addCondition("jiudianId", EConditionOperator.EQULS, jiudianId);
                dingdanQueryCondition.addCondition("id", EConditionOperator.EQULS, yudingId);
                dingdanQueryCondition.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
                MyExample dingdanMyExample = new MyExample(YwYudingDingdan.class, dingdanQueryCondition);
                List<YwYudingDingdan> yudingDingdanList = yudingDingdanMapper.selectByExample(dingdanMyExample);
                if (CollectionUtils.isEmpty(yudingDingdanList)) {
                    log.error("traceId[" + traceId + "]预订单不存在");
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    resultVO.setMessage("预订单不存在");
                    return resultVO;
                }

                yudingDingdan = yudingDingdanList.get(0);
                byte zhuangtai = yudingDingdan.getZhuangtai();
                if (zhuangtai != YwYudingDingdanConstants.ZHUANGTAI_YITIJIAO) {
                    log.error("traceId[" + traceId + "]该订单不能入住");
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    resultVO.setMessage("该订单不能入住");
                    return resultVO;
                }

                //修改预订单为已入住
                yudingDingdan.setZhuangtai(YwYudingDingdanConstants.ZHUANGTAI_YIRUZHU);
                int yudingdanRet = yudingDingdanMapper.updateByPrimaryKeySelective(yudingDingdan);
                if (yudingdanRet <= 0) {
                    log.error("traceId[" + traceId + "]预订单修改失败");
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    resultVO.setMessage("预订单修改失败");
                    return resultVO;
                }
            }

            //计算总金额
            int ruzhuzongrenshuTotal = 0;
            double dingdanzongjiaTotal = 0;
            double yingshoujineTotal = 0;
            double youhuijineTotal = 0;
            double shishoujineTotal = 0;

            List<TeamInHotelRoomVO> roomVOList = teamInHotelDTO.getRoomVOList();
            for (TeamInHotelRoomVO teamInHotelRoomVO : roomVOList) {
                int fangjianid = teamInHotelRoomVO.getFangjianId();
                //根据房间id，获取房间信息
                YwFangjian fangjian = fangjianMapper.selectByPrimaryKey(fangjianid);
                if (fangjian == null || fangjian.getShanchubiaoshi() != SysDicConstant.DELFLAG_0) {
                    log.error("traceId[" + traceId + "]房间[" + fangjianid + "]不可用");
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    resultVO.setMessage("房间[" + fangjianid + "]不可用");
                    return resultVO;
                }

                //根据房型id，获取房型信息
                int fangxingid = fangjian.getFangxingId();
                YwFangjianleixing fangjianleixing = fangjianleixingMapper.selectByPrimaryKey(fangxingid);

                double youhuijia = teamInHotelRoomVO.getYouhuijia().doubleValue();
                double mendianjia = fangjianleixing.getMendianjia().doubleValue();

                youhuijineTotal = youhuijineTotal + (mendianjia - youhuijia);
                dingdanzongjiaTotal = dingdanzongjiaTotal + youhuijia;
                yingshoujineTotal = yingshoujineTotal + youhuijia;
                shishoujineTotal = shishoujineTotal + youhuijia;

                List<TogetherInHotelInfoVO> togetherInHotelInfoVOList = teamInHotelRoomVO.getTogetherInHotelInfoVOList();
                ruzhuzongrenshuTotal = ruzhuzongrenshuTotal + togetherInHotelInfoVOList.size();
            }

            //写入订单表
            YwRuzhuDingdan ruzhuDingdan = new YwRuzhuDingdan();
            ruzhuDingdan.setHuiyuanId(-1);
            ruzhuDingdan.setDingdanhao(dingdanhao);
            ruzhuDingdan.setChuangjianshijian(new Date());
            ruzhuDingdan.setFukuanfangshi(teamInHotelDTO.getFukuanfangshi());
            ruzhuDingdan.setGengxinshijian(new Date());
            ruzhuDingdan.setJiezhangzhuangtai(YwRuzhuDingdanConstants.JIEZHANGZHUANGTAI_WEIJIE);
            ruzhuDingdan.setJiudianId(jiudianId);
            ruzhuDingdan.setKeyuanleixing(YwRuzhuDingdanConstants.KEYUANLEIXING_DANWEI);
            if (yudingDingdan == null) {
                ruzhuDingdan.setLaiyuan(YwRuzhuDingdanConstants.LAIYUAN_QIANTAI);
            } else {
                ruzhuDingdan.setYudingriqi(yudingDingdan.getYudingriqi());
                ruzhuDingdan.setLaiyuan(YwRuzhuDingdanConstants.LAIYUAN_WANGZHAN);
                ruzhuDingdan.setYudingdingdanhao(yudingDingdan.getDingdanhao());
                ruzhuDingdan.setYudingdingdanId(yudingDingdan.getId());
            }
            ruzhuDingdan.setTuanduimingcheng(teamInHotelDTO.getTuanduimingcheng());
            ruzhuDingdan.setLikaishijian(teamInHotelDTO.getLikaishijian());
            ruzhuDingdan.setRuzhuleixing(teamInHotelDTO.getRuzhuleixing());
            ruzhuDingdan.setRuzhushijian(teamInHotelDTO.getRuzhushijian());
            ruzhuDingdan.setRuzhuzongrenshu(ruzhuzongrenshuTotal);
            ruzhuDingdan.setShanchubiaoshi(SysDicConstant.DELFLAG_0);
            ruzhuDingdan.setYajin(teamInHotelDTO.getYajin());
            ruzhuDingdan.setDingdanzongjia(new BigDecimal(dingdanzongjiaTotal));
            ruzhuDingdan.setYingshoujine(new BigDecimal(yingshoujineTotal));
            ruzhuDingdan.setYouhuijine(new BigDecimal(youhuijineTotal));
            ruzhuDingdan.setShishoujine(new BigDecimal(shishoujineTotal));
            ruzhuDingdan.setYudingfangjianshu(roomVOList.size());
            ruzhuDingdan.setYudingleixing(YwRuzhuDingdanConstants.YUDINGLEIXING_TUANTI);
            ruzhuDingdan.setYudingrendianhua(teamInHotelDTO.getYudingrendianhua());
            ruzhuDingdan.setYudingrenshenfenId(guestId);
            ruzhuDingdan.setYudingrenxingming(teamInHotelDTO.getYudingrenxingming());
            ruzhuDingdan.setZhengjianhao(teamInHotelDTO.getZhengjianhao());
            ruzhuDingdan.setZhengjianleixing(teamInHotelDTO.getZhengjianleixing());
            ruzhuDingdan.setZhuangtai(YwRuzhuDingdanConstants.ZHUANGTAI_YIRUZHU);
            ruzhuDingdan.setBeizhu(teamInHotelDTO.getBeizhu());

            int dingdanRet = ruzhuDingdanMapper.insertSelective(ruzhuDingdan);
            if (dingdanRet <= 0) {
                log.error("traceId[" + traceId + "]写入入住单失败");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultVO.setMessage("写入入住单失败");
                return resultVO;
            }

            int dingdanId = ruzhuDingdan.getId();
            Date kaishishijian = teamInHotelDTO.getRuzhushijian();
            Date jieshushijian = teamInHotelDTO.getLikaishijian();

            //有预订单，解锁房间
            if (yudingDingdan != null) {
                QueryConditions fangjianQueryCondition = new QueryConditions();
                fangjianQueryCondition.addCondition("dingdanId", EConditionOperator.EQULS, yudingDingdan.getId());
                fangjianQueryCondition.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
                MyExample fangjianMyExample = new MyExample(YwYudingDingdanZixiang.class, fangjianQueryCondition);
                List<YwYudingDingdanZixiang> yudingDingdanZixiangList = yudingDingdanZixiangMapper.selectByExample(fangjianMyExample);

                for (YwYudingDingdanZixiang yudingDingdanZixiang : yudingDingdanZixiangList) {
                    QueryConditions fangtairiqijiluQueryCondition = new QueryConditions();
                    fangtairiqijiluQueryCondition.addCondition("jiudianId", EConditionOperator.EQULS, yudingDingdanZixiang.getJiudianId());
                    fangtairiqijiluQueryCondition.addCondition("fangjianId", EConditionOperator.EQULS, yudingDingdanZixiang.getFangjianId());
                    fangtairiqijiluQueryCondition.addCondition("kaishishijian", EConditionOperator.EQULS, yudingDingdanZixiang.getRuzhushijian());
                    fangtairiqijiluQueryCondition.addCondition("jieshushijian", EConditionOperator.EQULS, yudingDingdanZixiang.getLikaishijian());
                    fangtairiqijiluQueryCondition.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
                    fangtairiqijiluQueryCondition.addCondition("fangjianleibie", EConditionOperator.EQULS, YwFangtairiqijiluConstants.FANGJIANLEIBIE_FANGJIAN);
                    MyExample fangtairiqijiluMyExample = new MyExample(YwFangtairiqijilu.class, fangtairiqijiluQueryCondition);
                    YwFangtairiqijilu fangtairiqijilu = new YwFangtairiqijilu();
                    fangtairiqijilu.setShanchubiaoshi(SysDicConstant.DELFLAG_1);

                    int fangtaiRet = fangtairiqijiluMapper.updateByExampleSelective(fangtairiqijilu, fangtairiqijiluMyExample);
                    if (fangtaiRet <= 0) {
                        log.error("traceId[" + traceId + "]修改房态失败");
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        resultVO.setMessage("修改房态失败");
                        return resultVO;
                    }
                }
            }


            //根据选择的预抵时间、预离时间、房间，锁定房间
            for (TeamInHotelRoomVO teamInHotelRoomVO : roomVOList) {
                int fangjianId = teamInHotelRoomVO.getFangjianId();

                //根据房间id，获取房间信息
                YwFangjian fangjian = fangjianMapper.selectByPrimaryKey(fangjianId);

                int fangxingId = fangjian.getFangxingId();
                YwFangjianleixing fangjianleixing = fangjianleixingMapper.selectByPrimaryKey(fangxingId);

                //判断该房间是否被占用
                QueryConditions fangtairiqijiluQueryConditions = new QueryConditions();
                fangtairiqijiluQueryConditions.addCondition("jiudianId", EConditionOperator.EQULS, jiudianId);
                fangtairiqijiluQueryConditions.addCondition("fangjianId", EConditionOperator.EQULS, fangjianId);
                fangtairiqijiluQueryConditions.addCondition("shanchubiaoshi", EConditionOperator.EQULS, SysDicConstant.DELFLAG_0);
                fangtairiqijiluQueryConditions.addCondition("fangjianleibie", EConditionOperator.EQULS, YwFangtairiqijiluConstants.FANGJIANLEIBIE_FANGJIAN);
                MyExample fangtairiqijiluMyExample = new MyExample(YwFangtairiqijilu.class, fangtairiqijiluQueryConditions);

                QueryConditions shijianQueryConditions = new QueryConditions();
                shijianQueryConditions.addCondition("kaishishijian", kaishishijian, jieshushijian);
                shijianQueryConditions.addCondition("jieshushijian", kaishishijian, jieshushijian);
                Weekend weekend = fangtairiqijiluMyExample.createOrWeekend(YwFangtairiqijilu.class, shijianQueryConditions);
                weekend.and(fangtairiqijiluMyExample.getOredCriteria().get(0));

                List<YwFangtairiqijilu> fangtairiqijiluList = fangtairiqijiluMapper.selectByExample(weekend);
                if (!CollectionUtils.isEmpty(fangtairiqijiluList)) {
                    log.error("traceId[" + traceId + "]房间[" + fangjian.getFangjianbianhao() + "]已占用");
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    resultVO.setMessage("房间[" + fangjian.getFangjianbianhao() + "]已占用");
                    return resultVO;
                } else {
                    //写入房态日期记录
                    YwFangtairiqijilu fangtairiqijilu = new YwFangtairiqijilu();
                    fangtairiqijilu.setJiudianId(jiudianId);
                    fangtairiqijilu.setFangjianId(fangjianId);
                    fangtairiqijilu.setKaishishijian(kaishishijian);
                    fangtairiqijilu.setJieshushijian(jieshushijian);
                    fangtairiqijilu.setFangjianzhuangtai(YwFangtairiqijiluConstants.FANGJIANZHUANGTAI_ZHUJING);
                    fangtairiqijilu.setFangjianleibie(YwFangtairiqijiluConstants.FANGJIANLEIBIE_FANGJIAN);
                    fangtairiqijilu.setYewuyuanId(dingdanId);
                    fangtairiqijilu.setYewuleixing(YwFangtairiqijiluConstants.YEWULEIXING_SUOFANG);
                    fangtairiqijilu.setZhuangtai(YwFangtairiqijiluConstants.ZHUANGTAI_YOUXIAO);
                    fangtairiqijilu.setCaozuobiaoshi(YwFangtairiqijiluConstants.CAOZUOBIAOSHI_XITONGSUOFANG);
                    fangtairiqijilu.setBeizhu("入住锁房");
                    fangtairiqijilu.setChuangjianshijian(new Date());
                    fangtairiqijilu.setGengxinshijian(new Date());
                    fangtairiqijilu.setShanchubiaoshi(SysDicConstant.DELFLAG_0);
                    int fangtaiRet = fangtairiqijiluMapper.insertSelective(fangtairiqijilu);
                    if (fangtaiRet <= 0) {
                        log.error("traceId[" + traceId + "]写入房态日期记录失败");
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        resultVO.setMessage("写入房态日期记录失败");
                        return resultVO;
                    }

                    //写入订单子表
                    YwRuzhuDingdanZixiang ruzhuDingdanZixiang = new YwRuzhuDingdanZixiang();
                    ruzhuDingdanZixiang.setChuangjianshijian(new Date());
                    ruzhuDingdanZixiang.setDingdanhao(dingdanhao);
                    ruzhuDingdanZixiang.setDingdanId(dingdanId);
                    ruzhuDingdanZixiang.setFangjianId(fangjianId);
                    ruzhuDingdanZixiang.setFangjianhao(fangjian.getFangjianbianhao());
                    ruzhuDingdanZixiang.setFangxingId(fangjianleixing.getId());
                    ruzhuDingdanZixiang.setFangxingmingcheng(fangjianleixing.getFangxingmingcheng());
                    ruzhuDingdanZixiang.setGengxinshijian(new Date());
                    ruzhuDingdanZixiang.setJiudianId(jiudianId);
                    ruzhuDingdanZixiang.setLianxidianhua(teamInHotelDTO.getYudingrendianhua());
                    ruzhuDingdanZixiang.setLikaishijian(teamInHotelDTO.getLikaishijian());
                    ruzhuDingdanZixiang.setMendianjia(fangjianleixing.getMendianjia());
                    ruzhuDingdanZixiang.setRuzhushijian(teamInHotelDTO.getRuzhushijian());
                    ruzhuDingdanZixiang.setShanchubiaoshi(SysDicConstant.DELFLAG_0);
                    ruzhuDingdanZixiang.setShuliang(-1);
                    ruzhuDingdanZixiang.setRuzhuxingzhi((byte) -1);
                    ruzhuDingdanZixiang.setYouhuijia(teamInHotelRoomVO.getYouhuijia());
                    ruzhuDingdanZixiang.setYudingrenshenfenId(guestId);
                    ruzhuDingdanZixiang.setYudingrenzhengjianhao(teamInHotelDTO.getZhengjianhao());
                    ruzhuDingdanZixiang.setYudingrenzhengjianleixing(teamInHotelDTO.getZhengjianleixing());
                    ruzhuDingdanZixiang.setYudingxingming(teamInHotelDTO.getYudingrenxingming());
                    int zixiangRet = ruzhuDingdanZixiangMapper.insertSelective(ruzhuDingdanZixiang);

                    //如果写入失败，手动回滚事务
                    if (zixiangRet <= 0) {
                        log.error("traceId[" + traceId + "]写入入住单子表失败");
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        resultVO.setMessage("写入入住单子表失败");
                        return resultVO;
                    }

                    int ruzhuDingdanZixiangId = ruzhuDingdanZixiang.getId();

                    //循环同住人信息集合
                    List<TogetherInHotelInfoVO> togetherInHotelInfoVOList = teamInHotelRoomVO.getTogetherInHotelInfoVOList();
                    for (TogetherInHotelInfoVO togetherInHotelInfoVO : togetherInHotelInfoVOList) {
                        //保存宾客信息
                        YwZhengjianxinxi togetherYwZhengjianxinxi = new YwZhengjianxinxi();
                        togetherYwZhengjianxinxi.setShoujihao(CustomSecurity.encodeMobile(togetherInHotelInfoVO.getLianxidianhua()));
                        togetherYwZhengjianxinxi.setXingming(togetherInHotelInfoVO.getYudingxingming());
                        togetherYwZhengjianxinxi.setZhengjianhao(CustomSecurity.encodeIDCard(togetherInHotelInfoVO.getYudingrenzhengjianhao()));
                        togetherYwZhengjianxinxi.setZhengjianleixing(togetherInHotelInfoVO.getYudingrenzhengjianleixing());
                        togetherYwZhengjianxinxi.setJiudianId(jiudianId);
                        togetherYwZhengjianxinxi.setYewuleixing(YwZhengjianxinxiConstants.YEWULEIXING_RUZHUDINGDAN);
                        togetherYwZhengjianxinxi.setYewulaiyuanId(dingdanId);
                        int togetherGuestId = guestService.saveGuest(togetherYwZhengjianxinxi);

                        //写入同住人信息表
                        YwFangjiantongzhuren fangjiantongzhuren = new YwFangjiantongzhuren();
                        fangjiantongzhuren.setChuangjianshijian(new Date());
                        fangjiantongzhuren.setFangjianhao(fangjian.getFangjianbianhao());
                        fangjiantongzhuren.setFangjianId(fangjianId);
                        fangjiantongzhuren.setGengxinshijian(new Date());
                        fangjiantongzhuren.setRuzhuDingdanhao(dingdanhao);
                        fangjiantongzhuren.setRuzhuDingdanId(dingdanId + "");
                        fangjiantongzhuren.setRuzhuDingdanzixiangId(ruzhuDingdanZixiangId);
                        fangjiantongzhuren.setShanchubiaoshi(SysDicConstant.DELFLAG_0);
                        fangjiantongzhuren.setZhengjianId(togetherGuestId);
                        int tongzhurenRet = fangjiantongzhurenMapper.insertSelective(fangjiantongzhuren);
                        if (tongzhurenRet <= 0) {
                            log.error("traceId[" + traceId + "]写入同住人表失败");
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                            resultVO.setMessage("写入同住人表失败");
                            return resultVO;
                        }
                    }

                    //修改房间表状态
                    fangjian.setZhuangtai(YwFangjianConstants.ZHUANGTAI_ZHUJING);
                    int fangjianRet = fangjianMapper.updateByPrimaryKeySelective(fangjian);
                    if (fangjianRet <= 0) {
                        log.error("traceId[" + traceId + "]房间表修改失败");
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        resultVO.setMessage("房间表修改失败");
                        return resultVO;
                    }
                }
            }

            //写入订单收支记录
            YwDingdanshouzhijilu dingdanshouzhijilu = new YwDingdanshouzhijilu();
            dingdanshouzhijilu.setLeibie(YwDingdanshouzhijiluConstants.LEIBIE_DINGDANYAJIN);
            dingdanshouzhijilu.setFukuanfangshi(teamInHotelDTO.getFukuanfangshi());
            dingdanshouzhijilu.setJine(teamInHotelDTO.getYajin());
            dingdanshouzhijilu.setBeizhu("团队入住押金");
            dingdanshouzhijilu.setJiudianId(manageUserCache.getJiudianId());
            dingdanshouzhijilu.setChuangjianshijian(new Date());
            dingdanshouzhijilu.setDingdanhao(ruzhuDingdan.getDingdanhao());
            dingdanshouzhijilu.setDingdanId(ruzhuDingdan.getId());
            dingdanshouzhijilu.setGengxinshijian(new Date());
            dingdanshouzhijilu.setShanchubiaoshi(SysDicConstant.DELFLAG_0);
            dingdanshouzhijilu.setZhengjianId(ruzhuDingdan.getYudingrenshenfenId());
            dingdanshouzhijilu.setZhengjianleixing(ruzhuDingdan.getZhengjianleixing());
            dingdanshouzhijilu.setZhengjianhao(ruzhuDingdan.getZhengjianhao());
            dingdanshouzhijilu.setZhuangtai(YwDingdanshouzhijiluConstants.ZHUANGTAI_YOUXIAO);

            int shouzhiRet = dingdanshouzhijiluMapper.insertSelective(dingdanshouzhijilu);
            if (shouzhiRet <= 0) {
                log.error("traceId[" + traceId + "]收支记录写入失败");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultVO.setMessage("收支记录写入失败");
                return resultVO;
            }

            //写入操作日志
            YewuyonghuCaozuoRizhi yewuyonghuCaozuoRizhi = new YewuyonghuCaozuoRizhi("入住", "团队入住", "订单号[" + dingdanhao + "]");
            operateLogService.writeOperateLog(yewuyonghuCaozuoRizhi);

            TeamInHotelResultDTO teamInHotelResultDTO = new TeamInHotelResultDTO();
            teamInHotelResultDTO.setDingdanId(dingdanId);
            teamInHotelResultDTO.setDingdanhao(dingdanhao);

            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setMessage("入住成功");
            resultVO.setData(teamInHotelResultDTO);
        } catch (Exception e) {
            log.error("traceId[" + traceId + "]入住失败，系统异常", e);
            throw new BizException("入住失败，系统异常");
        } finally {
            commonRedisTemplate.delete(redisLockKey);
        }

        return resultVO;
    }
}
